using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using NoMana.Components.Camera;

namespace NoMana.Components.Lighting
{

    public class Spotlight
    {
        private readonly Game _owner;
        private readonly GameCamera _camera;

        public Effect LightEffect;

  
        private float _ambient;
        public float Ambient { get { return _ambient; } set { _ambient = value; LightEffect.Parameters["xAmbient"].SetValue(_ambient); } }

        private Vector3 _lightPosition;
        public Vector3 LightPosition { get { return _lightPosition; } set { _lightPosition = value; LightEffect.Parameters["xLightPosition"].SetValue(_lightPosition); } }


        private Vector3 _coneDirection;
        public Vector3 ConeDirection { get { return _coneDirection; } set { _coneDirection = value; LightEffect.Parameters["xConeDirection"].SetValue(_coneDirection); } }

        private float _coneAngle;
        public float ConeAngle { get { return _coneAngle; } set { _coneAngle = value; LightEffect.Parameters["xConeAngle"].SetValue(_coneAngle); } }

        private float _coneDecay;
        public float ConeDecay { get { return _coneDecay; } set { _coneDecay = value; LightEffect.Parameters["xConeDecay"].SetValue(_coneDecay); } }

        private float _lightStrength;
        public float LightStrength { get { return _lightStrength; } set { _lightStrength = value; LightEffect.Parameters["xLightStrength"].SetValue(_lightStrength); } }


        public Spotlight(Game owner, GameCamera camera, float ambient, Vector3 lightPosition, float coneAngle, float coneDecay, float lightStrength)
        {
            _owner = owner;
            _camera = camera;

            LightEffect = _owner.Content.Load<Effect>("Effects\\Lighting\\spotlight");

            LightEffect.CurrentTechnique = LightEffect.Techniques["SpotLight"];
         
            LightEffect.Parameters["xWorld"].SetValue(Matrix.Identity);
            LightEffect.Parameters["xView"].SetValue(_camera.ViewMatrix);
            LightEffect.Parameters["xProjection"].SetValue(_camera.ProjectionMatrix);

            Ambient = ambient;
            LightPosition = lightPosition;
            ConeAngle = coneAngle;
            ConeDecay = coneDecay;
            LightStrength = lightStrength;

        }

        public void LookAt(Vector3 position)
        {
            ConeDirection  = Vector3.Subtract(position, LightPosition);

        }

    }
}
